package com.jsjvm.server.dbgproxy.jdwp.cmdsets;

import java.io.IOException;

import com.jsjvm.server.dbgproxy.JDPABackEnd;
import com.jsjvm.server.dbgproxy.JDWPCommandSet;
import com.jsjvm.server.dbgproxy.jdwp.data.JDWPInputStream;
import com.jsjvm.server.dbgproxy.jdwp.data.JDWPOutputStream;
import com.jsjvm.server.dbgproxy.jdwp.data.JDWPRequestHeader;
import com.jsjvm.shared.vmcmds.VMCommand;

/**
 * Thread group command handlers: we have only one thread and one thread group
 * 
 * @author ctalau
 *
 */
@SuppressWarnings("serial")
public class JDWPThreadGroupCommandSet extends JDWPCommandSet {
    private static final int TG_NAME_CMD		= 1;
    private static final int TG_PARENT_CMD		= 2;
    private static final int TG_CHILDREN_CMD	= 3;

	public static final int THREAD_GROUP_REFERENECE_SET = 12;

	@Override
	public int getCommandSetNumber() {
		return THREAD_GROUP_REFERENECE_SET;
	}
	
	public JDWPThreadGroupCommandSet() {
		this.put(TG_NAME_CMD, new ThreadGroupName());
		this.put(TG_PARENT_CMD, new ThreadGroupParent());
		this.put(TG_CHILDREN_CMD, new ThreadGroupChildren());
	}
	
	private static class ThreadGroupName extends JDWPCommandHandler {
		@Override
		public VMCommand handleCommand(JDWPRequestHeader hdr, JDWPInputStream is,
				JDWPOutputStream os) throws IOException {
			os.appendString(JDPABackEnd.THREAD_GROUP_NAME);
			os.commitReplyPacket(hdr);
			return null;
		}
	}
	
	private static class ThreadGroupParent extends JDWPCommandHandler {
		@Override
		public VMCommand handleCommand(JDWPRequestHeader hdr, JDWPInputStream is,
				JDWPOutputStream os) throws IOException {
			os.appendID(0);	// top-level
			os.commitReplyPacket(hdr);
			return null;
		}
	}
	
	private static class ThreadGroupChildren extends JDWPCommandHandler {
		@Override
		public VMCommand handleCommand(JDWPRequestHeader hdr, JDWPInputStream is,
				JDWPOutputStream os) throws IOException {
			os.appendInt(0); 	// 0 thread children
			os.appendInt(0);	// 0 child groups
			os.commitReplyPacket(hdr);
			return null;
		}
	}
	

}
